package ru.isys.yoomee.ui;

import java.util.ArrayList;

import ru.isys.yoomee.R;
import ru.isys.yoomee.model.Yoo;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;

public class YoosTabActivity extends TabActivity {
	private ArrayList<Yoo> mYoos;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.yoos_tab_layout);

		mYoos = new ArrayList<Yoo>();
		mockYoos();

		initializeTabs();
	}

	private void initializeTabs() {

		TabHost tabHost = getTabHost();
		TabHost.TabSpec spec;
		Intent intent;

		intent = new Intent(this, YoosAllActivity.class);
		intent.putExtra(YoosAllActivity.YOOS_ARRAY_KEY, mYoos);
		
		spec = tabHost.newTabSpec("yoos_all").setIndicator(prepareTab(0)).setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent(this, YoosAllActivity.class);
		intent = setupMaleIntent(intent);
		spec = tabHost.newTabSpec("yoos_male").setIndicator(prepareTab(1)).setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent(this, YoosAllActivity.class);
		intent = setupFemaleIntent(intent);
		spec = tabHost.newTabSpec("yoos_female").setIndicator(prepareTab(2)).setContent(intent);
		tabHost.addTab(spec);

		tabHost.setCurrentTab(0);
	}

	private View prepareTab(int i) {
		View tabView = getLayoutInflater().inflate(R.layout.yoos_tab, null);
		TextView textView = (TextView) tabView.findViewById(R.id.yoos_tab_text);
		switch (i) {
		case 0: {
			textView.setText("All");
			break;
		}
		case 1: {
			textView.setText("Male");
			break;
		}
		case 2: {
			textView.setText("Female");
			break;
		}
		}

		return tabView;
	}

	private void mockYoos() {
		mYoos.add(new Yoo("Groupon", 12, 31, false));
		mYoos.add(new Yoo("FourSquare"));
		mYoos.add(new Yoo("Facebook"));
		mYoos.add(new Yoo("IBM"));
		mYoos.add(new Yoo("Starbucks", 12, 31, false));
		mYoos.add(new Yoo("Apple Store"));
		mYoos.add(new Yoo("Royal Albert Hall"));
		mYoos.add(new Yoo("Mc Donalds", 12, 31, false));
	}

	private Intent setupMaleIntent(Intent intent) {

		ArrayList<Yoo> yoos = new ArrayList<Yoo>();
		for (Yoo yoo : mYoos) {
			if (yoo.getSex()) {
				yoos.add(yoo);
			}
		}
		intent.putExtra(YoosAllActivity.YOOS_ARRAY_KEY, yoos);
		return intent;
	}

	private Intent setupFemaleIntent(Intent intent) {

		ArrayList<Yoo> yoos = new ArrayList<Yoo>();
		for (Yoo yoo : mYoos) {
			if (!yoo.getSex()) {
				yoos.add(yoo);
			}
		}
		intent.putExtra(YoosAllActivity.YOOS_ARRAY_KEY, yoos);
		return intent;
	}

}